''' CodeBot PT Practice #4 A remix of LineFollower1 ''' from botcore import * from time import sleep thresh = 1000 # Use a thresh value that works for your 'bot and line is_reflective = False # Set for black line SPEED = 30 SPEED_LIMIT = 0.8 motors.enable(True) def drive(left, right): motors.run(LEFT, left * SPEED_LIMIT) motors.run(RIGHT, right * SPEED_LIMIT) def calibrate(gap): global thresh, is_reflective if abs(gap) < 500: for i in range(3): leds.user(0b00011000) sleep(0.1) leds.user(0b00000000) sleep(0.1) else: is_reflective = line < ground thresh = round(ground + (gap/2)) for i in range(3): spkr.pitch(500) sleep(0.1) spkr.pitch(1000) sleep(0.1) spkr.off() # -- Main Program -- while True: if buttons.was_pressed(0): break elif buttons.was_pressed(1): sensors = ls.check(0) line = sensors[2] ground = sensors[0] gap = line - ground calibrate(gap) while True: vals = ls.check(thresh, is_reflective) leds.ls(vals) if vals == (1, 0, 0, 0, 0): drive(-20, 50) elif vals == (1, 1, 0, 0, 0): drive(0, 60) elif vals == (0, 1, 1, 0, 0): drive(40, 80) elif vals == (0, 0, 1, 1, 0): # Center drive(100, 100) elif vals == (0, 0, 0, 1, 0): drive(100, 80) elif vals == (0, 0, 0, 1, 0): drive(80, 40) elif vals == (0, 0, 0, 1, 1): drive(60, 0) elif vals == (0, 0, 0, 0, 1): drive(50, -20)